package com.esri.core.geometry;

import com.esri.core.geometry.RasterizedGeometry2D;
import com.esri.core.geometry.ScanlineRasterizer;
import com.github.mikephil.charting.utils.Utils;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes.dex */
public class RasterizedGeometry2DImpl extends RasterizedGeometry2D {
    int[] a;
    int b;
    int c;
    double d;
    double e;
    double f;
    double g;
    double h;
    Envelope2D i;
    Transformation2D j;
    int k;

    /* loaded from: classes.dex */
    public class ScanCallbackImpl extends ScanlineRasterizer.ScanCallback {
        int[] a;
        int b;
        int c;

        public ScanCallbackImpl(int[] iArr, int i) {
            RasterizedGeometry2DImpl.this = r1;
            this.b = i;
            this.a = iArr;
        }

        public void setColor(int i) {
            this.c = i;
        }

        @Override // com.esri.core.geometry.ScanlineRasterizer.ScanCallback
        public void flush(int[] iArr, int i) {
            for (int i2 = 0; i2 < i; i2++) {
                int i3 = i2 * 3;
                int i4 = iArr[i3 + 1];
                int i5 = iArr[i3 + 2];
                for (int i6 = iArr[i3]; i6 < i4; i6++) {
                    int[] iArr2 = this.a;
                    int i7 = (this.b * i5) + (i6 >> 4);
                    iArr2[i7] = iArr2[i7] | (this.c << ((i6 & 15) * 2));
                }
            }
        }
    }

    void a(ScanlineRasterizer scanlineRasterizer, MultiPathImpl multiPathImpl, double d) {
        Point2D[] point2DArr = new Point2D[4];
        for (int i = 0; i < 4; i++) {
            point2DArr[i] = new Point2D();
        }
        SegmentIteratorImpl querySegmentIterator = multiPathImpl.querySegmentIterator();
        double transform = this.j.transform(d) + 0.75d;
        Point2D point2D = new Point2D();
        Point2D point2D2 = new Point2D();
        Point2D point2D3 = new Point2D();
        Point2D point2D4 = new Point2D();
        Point2D point2D5 = new Point2D();
        while (querySegmentIterator.nextPath()) {
            while (querySegmentIterator.hasNextSegment()) {
                Segment nextSegment = querySegmentIterator.nextSegment();
                point2D4.x = nextSegment.getStartX();
                point2D4.y = nextSegment.getStartY();
                point2D5.x = nextSegment.getEndX();
                point2D5.y = nextSegment.getEndY();
                this.j.a(point2D4, point2D4);
                this.j.a(point2D5, point2D5);
                point2D.a(point2D5, point2D4);
                double length = point2D.length();
                if (length != Utils.DOUBLE_EPSILON) {
                    point2D.scale(transform / length);
                    point2D2.setCoords(-point2D.y, point2D.x);
                    point2D3.setCoords(point2D.y, -point2D.x);
                    point2D4.sub(point2D);
                    point2D5.add(point2D);
                    point2DArr[0].add(point2D4, point2D2);
                    point2DArr[1].add(point2D4, point2D3);
                    point2DArr[2].add(point2D5, point2D3);
                    point2DArr[3].add(point2D5, point2D2);
                    scanlineRasterizer.fillConvexPolygon(point2DArr, 4);
                }
            }
        }
    }

    int a(double d) {
        return (int) Math.round((d * this.d) + this.f);
    }

    int b(double d) {
        return (int) Math.round((d * this.e) + this.g);
    }

    public RasterizedGeometry2DImpl(Geometry geometry, double d, int i) {
        a((MultiVertexGeometryImpl) geometry._getImpl(), d, i);
    }

    public RasterizedGeometry2DImpl(MultiVertexGeometryImpl multiVertexGeometryImpl, double d, int i) {
        a(multiVertexGeometryImpl, d, i);
    }

    void a(MultiVertexGeometryImpl multiVertexGeometryImpl, double d, int i) {
        int max = Math.max((int) ((Math.sqrt(i) * 2.0d) + 0.5d), 64);
        this.c = max;
        this.b = ((max * 2) + 31) / 32;
        this.i = new Envelope2D();
        this.h = d;
        int i2 = this.c;
        int i3 = this.b;
        int i4 = 0;
        while (i2 >= 8) {
            i4 += i3 * i2;
            i2 /= 2;
            i3 = ((i2 * 2) + 31) / 32;
        }
        this.a = new int[i4];
        for (int i5 = 0; i5 < i4; i5++) {
            this.a[i5] = 0;
        }
        ScanlineRasterizer scanlineRasterizer = new ScanlineRasterizer();
        ScanCallbackImpl scanCallbackImpl = new ScanCallbackImpl(this.a, this.b);
        int i6 = this.c;
        scanlineRasterizer.setRenderingSurface(i6, i6, scanCallbackImpl);
        multiVertexGeometryImpl.queryEnvelope2D(this.i);
        double width = this.i.getWidth();
        double d2 = this.c;
        double height = this.i.getHeight();
        Double.isNaN(d2);
        if (width <= d2 * height) {
            this.i.getHeight();
            this.i.getWidth();
        }
        this.i.inflate(d, d);
        Envelope2D envelope2D = new Envelope2D();
        int i7 = this.c;
        Envelope2D construct = Envelope2D.construct(1.0d, 1.0d, i7 - 2, i7 - 2);
        envelope2D.setCoords(this.i.getCenter(), Math.max(construct.getWidth() * d, this.i.getWidth()), Math.max(construct.getHeight() * d, this.i.getHeight()));
        double max2 = Math.max(d, Math.min(envelope2D.getWidth() / construct.getWidth(), envelope2D.getHeight() / construct.getHeight()) * 0.5d);
        Transformation2D transformation2D = new Transformation2D();
        this.j = transformation2D;
        transformation2D.a(envelope2D, construct);
        Transformation2D transformation2D2 = new Transformation2D();
        int value = multiVertexGeometryImpl.getType().value();
        if (value == 8710) {
            scanCallbackImpl.setColor(2);
            scanlineRasterizer.setTransform(transformation2D2);
            scanlineRasterizer.fillPoints((MultiPointImpl) multiVertexGeometryImpl, max2);
        } else if (value == 25607) {
            scanCallbackImpl.setColor(2);
            scanlineRasterizer.setTransform(transformation2D2);
            a(scanlineRasterizer, (MultiPathImpl) multiVertexGeometryImpl._getImpl(), max2);
        } else if (value == 27656) {
            scanCallbackImpl.setColor(1);
            scanlineRasterizer.setTransform(this.j);
            scanlineRasterizer.fillMultiPath((MultiPathImpl) multiVertexGeometryImpl, false);
            scanCallbackImpl.setColor(2);
            scanlineRasterizer.setTransform(transformation2D2);
            a(scanlineRasterizer, (MultiPathImpl) multiVertexGeometryImpl._getImpl(), max2);
        }
        this.d = this.j.xx;
        this.e = this.j.yy;
        this.f = this.j.xd;
        this.g = this.j.yd;
        c();
    }

    void c() {
        int i = this.c;
        int i2 = this.b;
        int i3 = i * i2;
        int i4 = i / 2;
        int i5 = 0;
        int i6 = i2;
        int i7 = ((i4 * 2) + 31) / 32;
        int i8 = i3;
        while (i > 8) {
            for (int i9 = 0; i9 < i4; i9++) {
                int i10 = i9 * 2;
                int i11 = i10 + 1;
                for (int i12 = 0; i12 < i4; i12++) {
                    int i13 = i12 * 2;
                    int i14 = i13 + 1;
                    int i15 = i13 >> 4;
                    int i16 = (i13 & 15) * 2;
                    int i17 = i14 >> 4;
                    int i18 = (i14 & 15) * 2;
                    int[] iArr = this.a;
                    int i19 = i5 + (i6 * i10);
                    int i20 = i5 + (i6 * i11);
                    int i21 = ((iArr[i19 + i15] >> i16) & 3) | ((iArr[i19 + i17] >> i18) & 3) | ((iArr[i20 + i15] >> i16) & 3) | ((iArr[i20 + i17] >> i18) & 3);
                    int i22 = (i7 * i9) + i8 + (i12 >> 4);
                    iArr[i22] = (i21 << ((i12 & 15) * 2)) | iArr[i22];
                }
            }
            int i23 = i4 / 2;
            int i24 = ((i23 * 2) + 31) / 32;
            int i25 = (i7 * i4) + i8;
            int i26 = i4;
            i4 = i23;
            i = i26;
            i6 = i7;
            i7 = i24;
            i5 = i8;
            i8 = i25;
        }
    }

    @Override // com.esri.core.geometry.RasterizedGeometry2D
    public RasterizedGeometry2D.HitType queryPointInGeometry(double d, double d2) {
        int i;
        int a = a(d);
        int b = b(d2);
        if (a < 0 || a >= (i = this.c) || b < 0 || b >= i) {
            return RasterizedGeometry2D.HitType.OUTSIDE;
        }
        int i2 = (this.a[(this.b * b) + (a >> 4)] >> ((a & 15) * 2)) & 3;
        if (i2 == 0) {
            return RasterizedGeometry2D.HitType.OUTSIDE;
        }
        if (i2 == 1) {
            return RasterizedGeometry2D.HitType.INSIDE;
        }
        return RasterizedGeometry2D.HitType.BORDER;
    }

    @Override // com.esri.core.geometry.RasterizedGeometry2D
    public RasterizedGeometry2D.HitType queryEnvelopeInGeometry(Envelope2D envelope2D) {
        if (!envelope2D.intersect(this.i)) {
            return RasterizedGeometry2D.HitType.OUTSIDE;
        }
        int a = a(envelope2D.xmin);
        int a2 = a(envelope2D.xmax);
        int b = b(envelope2D.ymin);
        int b2 = b(envelope2D.ymax);
        int i = 0;
        if (a < 0) {
            a = 0;
        }
        if (b < 0) {
            b = 0;
        }
        int i2 = this.c;
        if (a2 >= i2) {
            a2 = i2 - 1;
        }
        if (b2 >= i2) {
            b2 = i2 - 1;
        }
        if (a > a2 || b > b2) {
            return RasterizedGeometry2D.HitType.OUTSIDE;
        }
        int max = Math.max(a2 - a, 1) * Math.max(b2 - b, 1);
        int i3 = this.b;
        int i4 = this.c;
        int i5 = i3;
        int i6 = max;
        int i7 = 0;
        while (true) {
            if (i6 < 32 || i4 < 16) {
                for (int i8 = b; i8 <= b2; i8++) {
                    for (int i9 = a; i9 <= a2; i9++) {
                        i7 = (this.a[((i5 * i8) + i) + (i9 >> 4)] >> ((i9 & 15) * 2)) & 3;
                        if (i7 > 1) {
                            return RasterizedGeometry2D.HitType.BORDER;
                        }
                    }
                }
                if (i7 == 0) {
                    return RasterizedGeometry2D.HitType.OUTSIDE;
                }
                if (i7 == 1) {
                    return RasterizedGeometry2D.HitType.INSIDE;
                }
            }
            i += i5 * i4;
            i4 /= 2;
            i5 = ((i4 * 2) + 31) / 32;
            a /= 2;
            b /= 2;
            a2 /= 2;
            b2 /= 2;
            i6 = Math.max(a2 - a, 1) * Math.max(b2 - b, 1);
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    @Override // com.esri.core.geometry.RasterizedGeometry2D
    public double a() {
        return this.h;
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    @Override // com.esri.core.geometry.RasterizedGeometry2D
    public int b() {
        return this.c * this.b;
    }
}
